const util = require('../../static/js/util.js');
const {
  phoneExp,
} = require('../../../common/utils/util.js');

Page({
  data: {
    isPassword: true,
    isConfirmPassword: true,
    isCode: true,
    password: '',
    confirmPassword: '',
    phone: '',
    code: '',
    time: 60,
    imgUrl: util.getImgUrl(),
  },
  isSendCode: true,
  isLogin: true,
  onLoad() {},
  bindInputValue(e) {
    const id = e.currentTarget.dataset.id;
    const m = {};
    m[id] = e.detail.value;
    this.setData(m);
  },

  changePassword(e) {
    const id = e.currentTarget.dataset.id;
    const m = {};
    m[id] = !this.data[id];
    this.setData(m);
  },
  sendCode() {
    let {
      phone,
      time,
    } = this.data;
    let data = {};
    if (phone === '') {
      wx.showToast({
        title: `手机号不能为空`,
        icon: 'none',
      });
      return false;
    }
    if (!phoneExp.mobile.test(phone)) {
      wx.showToast({
        title: '请填写正确手机号码',
        icon: 'none',
      });
      return;
    }

    if (!this.isSendCode) return;
    this.isSendCode = false;
    util.request({
      url: 'entry/wxapp/user.authentication.getcode',
      data: {
        mobile: phone,
      },
      success: (res) => {
        this.setData({
          isCode: false,
        });
        let count = setInterval(() => {
          time = time - 1;
          data.time = time;
          if (time === 0) {
            clearInterval(count); // 清除倒计时
            data.time = 60; // 设置倒计时时间
            data.isCode = true;
          }
          this.setData(data);
        }, 1000);
      },
      complete: () => {
        this.isSendCode = true;
      },
    });
  },
  resetPassword(e) {
    const {
      confirmPassword,
      password,
      phone,
      code,
    } = this.data;
    if (phone === '') {
      wx.showToast({
        title: `手机号不能为空`,
        icon: 'none',
      });
      return false;
    }
    if (!phoneExp.mobile.test(phone)) {
      wx.showToast({
        title: '请填写正确手机号码',
        icon: 'none',
      });
      return;
    }
    if (code === '') {
      wx.showToast({
        title: `验证码不能为空`,
        icon: 'none',
      });
      return false;
    }
    if (password === '') {
      wx.showToast({
        title: `密码不能为空`,
        icon: 'none',
      });
      return false;
    }
    if (confirmPassword === '') {
      wx.showToast({
        title: `确认密码不能为空`,
        icon: 'none',
      });
      return false;
    }
    if (confirmPassword !== password) {
      wx.showToast({
        title: `密码和确认密码不一样`,
        icon: 'none',
      });
      return false;
    }
    if (!this.isLogin) return;
    this.isLogin = false;
    util.request({
      url: 'entry/wxapp/user.authentication.changepwd',
      data: {
        mobile: phone,
        pwdCode: code,
        pwdNew: password,
        repwdNew: confirmPassword,
      },
      success: (res) => {
        wx.showToast({
          title: '重置密码成功',
          mask: true,
          success: () => {
            setTimeout(() => {
              wx.navigateBack();
            }, 1000);
          },
        });
      },
      complete: () => {
        this.isLogin = true;
      },
    });
  },
});